<?php
	class Tk_accounts_subject extends MY_controller{

		function __construct(){
			parent::__construct();
			$this->load->model('Tk_accounts_subject_model');
		}

		function index($pid = 0, $limit = 0, $iframe = 0){
			//获取顶级
			$top_subject = $this->Tk_accounts_subject_model->get_top_subject_list();
			$this->smarty->assign("top_subject", $top_subject);


			$subject = $this->Tk_accounts_subject_model->get_subject_list($pid);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("pid", $pid);

			$this->smarty->assign("action", '题库分录题科目列表');
			$this->smarty->assign("name", '');
			$this->smarty->assign("pagination", '');
			$this->smarty->assign("current_url", '/tk_accounts_subject');
			if($iframe == 1){
				$this->smarty->display("tk_accounts_subject/iframe_list.html");
			}else{
				$this->smarty->display("tk_accounts_subject/list.html");
			}
		}



		function add(){
			$post_data = $this->input->post();
			if($post_data){
				unset($post_data['id']);
				$this->Tk_accounts_subject_model->add($post_data);
				header("Location:/tk_accounts_subject/index/{$post_data['pid']}");exit;
			}
			$this->smarty->assign("parent_subject", "顶级分类");

			//获取顶级
			$top_subject = $this->Tk_accounts_subject_model->get_top_subject_list();
			$this->smarty->assign("top_subject", $top_subject);

			$this->smarty->assign("post_url", '/tk_accounts_subject/add');

			$this->smarty->assign("action", '添加分录题科目');
			$this->smarty->display("tk_accounts_subject/add.html");
		}

		function edit($id){
			$post_data = $this->input->post();
			if($post_data){
				
				$this->Tk_accounts_subject_model->edit($post_data);
				header("Location:/tk_accounts_subject/index/{$post_data['pid']}");exit;
			}

			//科目
			$subject = $this->Tk_accounts_subject_model->get_subject_by_id($id);
			$parent_subject_name = "顶级分类";//科目上级分类名称

			if(!empty($subject) && $subject['pid']>0){
				$parent_subject = $this->Tk_accounts_subject_model->get_subject_by_id($subject['pid']);
				if($parent_subject){
					$parent_subject_name = $parent_subject['title'];
					
				}
			}
			$this->smarty->assign("parent_subject", $parent_subject_name);

			
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("post_url", "/tk_accounts_subject/edit/{$id}");
			$this->smarty->assign("action", '编辑分录题科目');
			$this->smarty->display("tk_accounts_subject/add.html");
		}

		function delete($id){
			$subject = $this->Tk_accounts_subject_model->get_subject_by_id($id);

			$this->Tk_accounts_subject_model->delete($id);
			header("Location:/tk_accounts_subject/index/{$subject['pid']}");exit;
		}

		function get_name_by_id($id){
			$subject = $this->Tk_accounts_subject_model->get_subject_by_id($id);
			print_r(empty($subject)?"":$subject['title']);exit;
		}

		function iframe_list2($value='')
		{
			$subject = $this->Tk_accounts_subject_model->get_top_subject_list();
			if($subject){
				foreach ($subject as $key => $value) {
					$subject[$key]['children'] = $this->get_subject_children($value['id']);
				}
			}
			$this->smarty->assign("subject", $subject);
			$this->smarty->display("tk_accounts_subject/iframe_list2.html");
		}


		function iframe_list(){
			$subject = $this->Tk_accounts_subject_model->get_top_subject_list();
			if($subject){
				foreach ($subject as $key => $value) {
					$subject[$key]['children'] = $this->get_subject_children($value['id']);
				}
			}
			$this->smarty->assign("subject", $subject);
			$this->smarty->display("tk_accounts_subject/iframe_list.html");
		}

		function get_subject_children($id){
			$children = $this->Tk_accounts_subject_model->get_subject_list_by_pid($id);
			return $children;
		}

		function ajax_get_subject_children(){
			$post_data = $this->input->post();
			if(is_ajax_request() && !empty($post_data['pid'])){
				$children = $this->get_subject_children($post_data['pid']);
				// echo $this->db->last_query();
				if($children) $code = 1;
				else $code = 0;
				echo json_encode(array("code" => $code, "children" => $children));
			}
		}
	}